package edu.kit.aifb.bowsim.index;

import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.index.Term;

public class TfIdfVector {
	Map<Term, Double> _data;
	
	public TfIdfVector() {
		_data = new HashMap<Term, Double>();
	}
	
	public void add(Term t, double val) {
		_data.put(t, val);
	}
	
	public double cosSim(TfIdfVector v) {
		double result = dotProduct(v) / (vectorNorm() * v.vectorNorm());
		
		return result;
	}
	
	private double vectorNorm() {
		double result = 0.0;

		for (double d : _data.values()) {
			result += d*d;
		}
		
		return Math.sqrt(result);
	}
	
	private double dotProduct(TfIdfVector v) {
		double result = 0.0;
		
		for (Term t : v._data.keySet()) {
			if (_data.containsKey(t)) {
				result += v._data.get(t) * _data.get(t);
			}
		}
		
		return result;
	}
	
	public String toString() {
		return _data.toString();
	}
}